﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Chowpoo.Cache;
using CourseSystem.Common.Models;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;

namespace CourseSystem.Common.Helpers
{
    public class ExamLibHelper
    {
        public static List<SelectListItem> GetLib(bool needDef = true)
        {
            var colList = ExamLibBLL.GetAll();
            var re = colList.Select(item => new SelectListItem { Value = item.Id.ToString(), Text = item.Title }).ToList();
            if (needDef)
                re.Insert(0, new SelectListItem { Text = "请选择", Value = "-1" });
            return re;
        }

        public static List<int> GetOrderList(int eid, int type, int userId, int confirmCount, bool needWash = false)
        {
            var key = eid + "_" + type + "_" + userId;
            var info = Cache2.GetCache(key) as QuestionTypeModel;
            //重新生成
            if (info == null || info.OrderList.Count != confirmCount)
            {
                var reList = GetRanList(confirmCount, needWash);
                Cache2.InsertNormalSlidingCache(key, new QuestionTypeModel { OrderList = reList }, 3600 * 5);
                return reList;
            }
            return info.OrderList;
        }

        public static List<int> GetRanList(int count, bool needWash = false)
        {
            var reList = new List<int>(count);
            //构建顺序数组
            for (int i = 0; i < count; i++)
            {
                reList.Add(i);
            }
            if (!needWash)
                return reList;
            //洗牌
            var random = new Random();
            for (int i = 1; i < reList.Count; i++)
            {
                var j = random.Next(0, i + 1);
                int temp = reList[i];
                reList[i] = reList[j];
                reList[j] = temp;
            }
            return reList;
        }

        public static string GetNameByType(int type)
        {
            switch (type)
            {
                case 1:
                    return "作业";
                case 0:
                    return "练习";
                case 4:
                    return "考试";
            }
            return "NotFound";
        }

        public static string GetItemScore(int id, int score, int now)
        {
            /*<select name="type" id="type">
                         <option value="-1">请选择</option>
                         <option value="0">作业</option>
                         <option value="1">练习</option>
                         <option value="2">考试</option>
                     </select>*/
            var re = "<select name=\"score" + id + "\" >{0}</select>";
            const string option = "<option value=\"{0}\" {1}>{0}</option>";
            var options = "";
            for (int i = 0; i <= score; i++)
            {
                options = options + string.Format(option, i, i == now ? "selected=\"selected\"" : "");
            }
            return string.Format(re, options);
        }

        public static string GetExamName(ExaminationInfo info)
        {
            if (info == null)
                return "";
            if (info.Type < 2)
            {
                return info.SectionInfo.ChapterInfo.Name + " " + info.SectionInfo.Name + " " + GetNameByType(info.Type);
            }
            return info.Title;
        }

        public static int GetScoreTran(int score, int sumScore)
        {
            if (sumScore == 0 || score == 0)
                return 0;
            return (int)(((float)score / sumScore) * 100);
        }

        public static bool ChangeItemScore(int itemId)
        {
            var needList = ExaminationItemBLL.GetList(itemId);
            foreach (var item in needList)
            {
                ExaminationBLL.UpdateExaminationScore(item.Eid);
                if (item.ExaminationInfo.Type == 4 && item.ExaminationInfo.Score != 100 && item.ExaminationInfo.Status == 1)
                {
                    item.ExaminationInfo.Status = 0;
                    ExaminationBLL.Edit(item.ExaminationInfo);
                }
            }
            return true;
        }

        public static bool ChangeItemScoreByDel(int[] ids)
        {
            foreach (var item in ids)
            {
                ExaminationBLL.UpdateExaminationScore(item);
                var infoItem = ExaminationBLL.Get(item);
                if (infoItem != null)
                    if (infoItem.Type == 4 && infoItem.Score != 100 && infoItem.Status == 1)
                    {
                        infoItem.Status = 0;
                        ExaminationBLL.Edit(infoItem);
                    }
            }
            return true;
        }

        public static bool IsCanExam()
        {
            var sectionCount = SectionBLL.GetCount();
            var learnCount = SectionLearnBLL.GetLearnCount(UserHelper.Student.Id);
            return (float)learnCount / sectionCount > 0.7;
        }
    }
}
